package com.gxuwz.endrskyweather;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.viewpager2.widget.ViewPager2;
import com.gxuwz.endrskyweather.adapter.WeatherPagerAdapter;
import com.gxuwz.endrskyweather.model.City;
import com.gxuwz.endrskyweather.viewmodel.CityManagerViewModel;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import java.util.ArrayList;
import java.util.List;

public class WeatherFragment extends Fragment {
    private ViewPager2 cityViewPager;
    private TabLayout cityTabLayout;
    private WeatherPagerAdapter weatherPagerAdapter;
    private CityManagerViewModel cityManagerViewModel;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, 
                           @Nullable ViewGroup container,
                           @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_weather, container, false);
        // 初始化 ViewModel
        cityManagerViewModel = new ViewModelProvider(requireActivity()).get(CityManagerViewModel.class);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        cityViewPager = view.findViewById(R.id.city_view_pager);
        cityTabLayout = view.findViewById(R.id.city_tabs_container);
        //LinearLayout emptyState = view.findViewById(R.id.empty_state);
        
        // 设置 ViewPager adapter
        weatherPagerAdapter = new WeatherPagerAdapter(this);
        cityViewPager.setAdapter(weatherPagerAdapter);

        // 主动加载数据
        cityManagerViewModel.loadDefaultCity();
        cityManagerViewModel.loadNonDefaultCities();
        
        // 合并城市数据观察
        cityManagerViewModel.getCombinedCities().observe(getViewLifecycleOwner(), cities -> {
            
            if (cities.isEmpty()) {
                Toast.makeText(getContext(), "请添加城市数据", Toast.LENGTH_SHORT).show();
            }
            
            weatherPagerAdapter.setCities(cities);

            // 安全更新Tab
            if (cityTabLayout.getTabCount() != cities.size()) {
                cityTabLayout.removeAllTabs();
                for (City cityName : cities) {
                    cityTabLayout.addTab(cityTabLayout.newTab().setText(cityName.getAreaCn()));
                }
            } else {
                for (int i = 0; i < cityTabLayout.getTabCount(); i++) {
                    TabLayout.Tab tab = cityTabLayout.getTabAt(i);
                    if (tab != null && i < cities.size()) {
                        tab.setText(cities.get(i).getAreaCn());
                    }
                }
            }
        });

        // 同步 ViewPager 和 TabLayout
        new TabLayoutMediator(cityTabLayout, cityViewPager, (tab, position) -> {
            tab.setText(weatherPagerAdapter.getCities().get(position));
        }).attach();

    }
}
